package com.ltt.controller;

import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.Attention;
import com.ltt.dao.pojo.User;
import com.ltt.dao.vo.Result;
import com.ltt.dao.vo.UserInfoVo;
import com.ltt.service.AttentionService;
import com.ltt.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/4 9:44
 */
@Api(tags = "关注模块")
@RestController
@RequestMapping("/attention")
public class AttentionController {
    @Autowired
    private AttentionService attentionService;

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("关注对方")
    @PostMapping("/insert")
    public Result insertAttention(String openid) {
        Attention attention = new Attention();
        attention.setFromOpenid(UserThreadLocal.get().getOpenid());
        attention.setToOpenid(openid);

        Attention tmp = attentionService.findAttention(attention);

        if(tmp == null){
            attentionService.insertAttention(attention);
        }else {
            attentionService.addAttention(attention);
        }
        return Result.successWithoutData();
    }

    @ApiOperation("取消关注")
    @PostMapping("/cancel")
    public Result cancelAttention(String openid) {
        Attention attention = new Attention();
        attention.setFromOpenid(UserThreadLocal.get().getOpenid());
        attention.setToOpenid(openid);

        attentionService.cancelAttention(attention);
        return Result.successWithoutData();
    }

    @ApiOperation("找到关注的所有用户")
    @PostMapping("/findAllUser")
    public Result findAllAttentionUser() {
        List<UserInfoVo> allAttentionUser = attentionService.findAllAttentionUser(UserThreadLocal.get().getOpenid());
        for (UserInfoVo userInfoVo : allAttentionUser) {
            User userById = userInfoService.findUserById(userInfoVo.getOpenid());
            BeanUtils.copyProperties(userById,userInfoVo);
        }
        return Result.successWithData(allAttentionUser);
    }

    @ApiOperation("找到自己的所有粉丝")
    @PostMapping("/findAllFans")
    public Result findAllFans() {
        List<UserInfoVo> allFansUser = attentionService.findAllFansUser(UserThreadLocal.get().getOpenid());
        for (UserInfoVo userInfoVo : allFansUser) {
            User userById = userInfoService.findUserById(userInfoVo.getOpenid());
            BeanUtils.copyProperties(userById,userInfoVo);

            Attention attention = new Attention();
            attention.setFromOpenid(UserThreadLocal.get().getOpenid());
            attention.setToOpenid(userInfoVo.getOpenid());
            Attention attention1 = attentionService.isAttention(attention);

            if(attention1 != null){
                userInfoVo.setAttention(1);
            }
        }
        return Result.successWithData(allFansUser);
    }

    @ApiOperation("是否已经关注")
    @PostMapping("/isAttention")
    public Result isAttention(@RequestParam String openid){
        Attention attention = new Attention();
        attention.setFromOpenid(UserThreadLocal.get().getOpenid());
        attention.setToOpenid(openid);
        Attention attention1 = attentionService.isAttention(attention);

        if(attention1 == null){
            return Result.successWithData("false");
        }else {
            return Result.successWithData("true");
        }
    }

}
